package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.BusinessRegisterMapper;
import com.aaa.mrcai.pojo.entity.BussinessInfo;
import com.aaa.mrcai.pojo.entity.Store;
import com.aaa.mrcai.pojo.entity.UserInfo;
import com.aaa.mrcai.pojo.vo.BusinessIntoVO;
import com.aaa.mrcai.pojo.vo.BusinessPicVO;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.BusinessRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @ Author: Lining
 * @ Date: 2021/12/10 16:42
 */
@Service
public class BusinessRegisterServiceImpl implements BusinessRegisterService {

    @Autowired
    private BusinessRegisterMapper businessRegisterMapper;

    @Autowired
    private ServletContext servletContext;

    @Override
    public AjaxResult addInfo(BussinessInfo bussinessInfo) {

        List<BussinessInfo> businessExist = businessRegisterMapper.isBusinessExist(bussinessInfo);
        if(businessExist.size()>0){
            return AjaxResult.defeated("该身份证已被注册为商家");
        }
        else{
            //获取登录时的账号密码手机号，(t_userInfo和t_userLogin中的内容)
            UserInfo user = (UserInfo) servletContext.getAttribute("user");
            //将手机号存入商家信息中
            bussinessInfo.setTelephone(user.getUserLogin().getTelephone());
            bussinessInfo.setPayaccount(user.getUserLogin().getTelephone());
            int i = businessRegisterMapper.addBusiness(bussinessInfo);
            if(i>0){
                //将存入businessInfo里的id，手机号，支付宝号，和身份证存入session
                servletContext.setAttribute("addBusinessId",bussinessInfo);
                businessRegisterMapper.updateBusinessByid(bussinessInfo);
                System.out.println("存入的seessionbussinessInfo:"+bussinessInfo);
                return AjaxResult.success("添加商家基本成功");
            }
            else{
                return AjaxResult.defeated("添加商家基本失败");
            }
        }
    }

    @Override
    public AjaxResult addStore(BusinessIntoVO businessIntoVO) {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        businessIntoVO.setTelephone(user.getUserLogin().getTelephone());
        Long businessExistByid = businessRegisterMapper.isBusinessExistByid(businessIntoVO);

        if(businessExistByid>0){
            return AjaxResult.defeated("该商家已被有店铺");
        }

        List<Store> storeExist = businessRegisterMapper.isStoreExist(businessIntoVO);
        if(storeExist.size()>0){
            return AjaxResult.defeated("该市场已存在该店铺");
        }
        else {
            int i = businessRegisterMapper.addStore(businessIntoVO);
            System.out.println("hahahhaha"+businessIntoVO);
            if(i>0) {
                Integer id = businessIntoVO.getId();
                //设置申请日期
                LocalDate date = LocalDate.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                businessIntoVO.setApplicationdate(date.format(formatter));
                //设置sid为添加到store中的id
                businessIntoVO.setSid(id);
                String telephone = user.getUserLogin().getTelephone();
                businessIntoVO.setTelephone(telephone);
                businessRegisterMapper.addBusinessSid(businessIntoVO);
                return AjaxResult.success("成功添加店铺");
            }
            else{
                return AjaxResult.defeated("添加店铺失败");
            }
        }
    }

    @Override
    public AjaxResult getState(BussinessInfo bussinessInfo) {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        bussinessInfo.setTelephone(user.getUserLogin().getTelephone());
        BussinessInfo state = businessRegisterMapper.getState(bussinessInfo);
        return AjaxResult.success(state);
    }

    @Override
    public AjaxResult addPic1(BusinessPicVO businessPicVO) {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        businessPicVO.setId(user.getUserLogin().getBid());
        int i = businessRegisterMapper.addPic1(businessPicVO);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }
    @Override
    public AjaxResult addPic2(BusinessPicVO businessPicVO) {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        businessPicVO.setId(user.getUserLogin().getBid());
        int i = businessRegisterMapper.addPic2(businessPicVO);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }
    @Override
    public AjaxResult addPic3(BusinessPicVO businessPicVO) {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        businessPicVO.setId(user.getUserLogin().getBid());
        int i = businessRegisterMapper.addPic3(businessPicVO);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }
    @Override
    public AjaxResult addPic4(BusinessPicVO businessPicVO) {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        businessPicVO.setId(user.getUserLogin().getBid());
        int i = businessRegisterMapper.addPic4(businessPicVO);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }




}
